/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package javaftp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
 
/**
 * A program demonstrates how to upload files from local computer to a remote
 * FTP server using Apache Commons Net API.
 * @author www.codejava.net
 */
public class FTPDownloadFileDemo {
 
    public static void ftpdownloadfile() {
        String server = "www.myserver.com";
        int port = 21;
        String user = "user";
        String pass = "pass";
 
        FTPClient ftpClient = new FTPClient();
        try { 
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
 
            // APPROACH #1: using retrieveFile(String, OutputStream)
            String remoteFile1 = "/test/video.mp4";
            File downloadFile1 = new File("D:/Downloads/video.mp4");
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
            boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
            outputStream1.close();
 
            if (success) {
                System.out.println("File #1 has been downloaded successfully.");
            }
 
            // APPROACH #2: using InputStream retrieveFileStream(String)
            String remoteFile2 = "/test/song.mp3";
            File downloadFile2 = new File("D:/Downloads/song.mp3");
            OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
            InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2);
            byte[] bytesArray = new byte[4096];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(bytesArray)) != -1) {
                outputStream2.write(bytesArray, 0, bytesRead);
            }
 
            success = ftpClient.completePendingCommand();
            if (success) {
                System.out.println("File #2 has been downloaded successfully.");
            }
            outputStream2.close();
            inputStream.close();
 
        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public StringBuilder revisar_ficheros_vuelos(String ruta){
    String directorio_expedientes = ruta;
    String ruta_archivo_expedientes;
    File carpeta_expedientes = new File(directorio_expedientes);
    String[] lista = carpeta_expedientes.list();
    StringBuilder semana_años = new StringBuilder();
    String semana_año = null;
        for (int z = 0; z < lista.length; z++) {
            ruta_archivo_expedientes = directorio_expedientes + "\\" + lista[z];
            String fecha_archivo = ruta_archivo_expedientes.substring(0, ruta_archivo_expedientes.length()-5);
             if (ruta_archivo_expedientes.contains("Vuelos")){
            semana_años.append(fecha_archivo);
             }
               if (z != lista.length - 1) {
                semana_años.append(",");
            }
        }
    
    return semana_años;
    
    }
     public StringBuilder revisar_ficheros_expedientes(String ruta){
    String directorio_expedientes = ruta;
    String ruta_archivo_expedientes;
    File carpeta_expedientes = new File(directorio_expedientes);
    String[] lista = carpeta_expedientes.list();
    StringBuilder semana_años = new StringBuilder();
    String semana_año = null;
        for (int z = 0; z < lista.length; z++) {
            ruta_archivo_expedientes = directorio_expedientes + "\\" + lista[z];
            String fecha_archivo = ruta_archivo_expedientes.substring(0, ruta_archivo_expedientes.length()-10);
             if (ruta_archivo_expedientes.contains("Expediente")){
            semana_años.append(fecha_archivo);
             }
               if (z != lista.length - 1) {
                semana_años.append(",");
            }
        }
    return semana_años;
    
    }
    
}